const friendLinkService = require('@/services/friend_link.service')
const JsonResult = require('@/utils/httpResponse.util')

class FriendLinkController {
  async findPage (req, response) {
    try {
      const data = await friendLinkService.findAll()
      JsonResult.success({
        req,
        response,
        data,
        message: '查询成功'
      })
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '查询失败' })
    }
  }

  async createFriendLink (req, response) {
    try {
      const data = await friendLinkService.insertFriendLink(req.body)
      if (data?.errorMessage) {
        return JsonResult.fail({ req, response, message: data.errorMessage })
      }
      JsonResult.success({
        req,
        response,
        message: '创建成功'
      })
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '创建失败' })
    }
  }

  async updateFriendLink (req, response) {
    try {
      const { id, ...attrs } = req.body
      const data = await friendLinkService.updateFriendLink(id, attrs)
      if (data?.errorMessage) {
        return JsonResult.fail({ req, response, message: data.errorMessage })
      }
      JsonResult.success({
        req,
        response,
        message: '更新成功'
      })
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '更新失败' })
    }
  }

  async deleteFriendLinks (req, response) {
    try {
      const ids = req.body.ids
      const data = await friendLinkService.deleteFriendLinks(ids)
      if (data?.errorMessage) {
        return JsonResult.fail({ req, response, message: data.errorMessage })
      }
      JsonResult.success({
        req,
        response,
        message: '删除成功'
      })
    } catch (error) {
      JsonResult.fail({ req, response, error, message: '删除失败' })
    }
  }
}

module.exports = new FriendLinkController()
